# Generated Cmake Pico project file

cmake_minimum_required(VERSION 3.13)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Initialise pico_sdk from installed location
# (note this can come from environment, CMake cache etc)

# == DO NEVER EDIT THE NEXT LINES for Raspberry Pi Pico VS Code Extension to work ==
if(WIN32)
   set(USERHOME $ENV{USERPROFILE})
else()
    set(USERHOME $ENV{HOME})
endif()
set(PICO_SDK_PATH ${USERHOME}/.pico-sdk/sdk/2.0.0)
set(PICO_TOOLCHAIN_PATH ${USERHOME}/.pico-sdk/toolchain/13_2_Rel1)
set(pioasm_HINT ${USERHOME}/.pico-sdk/tools/2.0.0/pioasm)
if(EXISTS ${pioasm_HINT})
    set(pioasm_DIR ${pioasm_HINT})
endif()
set(picotool_HINT ${USERHOME}/.pico-sdk/picotool/2.0.0/picotool)
if(EXISTS ${picotool_HINT})
    set(picotool_DIR ${picotool_HINT})
endif()
if(PICO_TOOLCHAIN_PATH MATCHES "RISCV")
    set(PICO_PLATFORM rp2350-riscv CACHE STRING "Pico Platform")
    if(PICO_TOOLCHAIN_PATH MATCHES "COREV")
        set(PICO_COMPILER pico_riscv_gcc_zcb_zcmp)
    endif()
endif()
# ====================================================================================
set(PICO_BOARD pico CACHE STRING "Board type")

# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)

if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0")
  message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()

project(tobudos C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# Add executable. Default name is the project name, version 0.1

set(TobudOS_ROOT_DIR ../..)
set(TobudOS_KNL_DIR ${TobudOS_ROOT_DIR}/kernel)

set(TobudOS_PORT_SRC
    ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/cortex-m0+/gcc/port_c.c
    ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/cortex-m0+/gcc/port_s.S
    ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/common/tos_cpu.c    
    ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/common/tos_fault.c    
)

file(GLOB TobudOS_SYS_SRC 
    ${TobudOS_KNL_DIR}/core/*.c
    ${TobudOS_KNL_DIR}/osal/cmsis_os/cmsis_os.c
    ${TobudOS_ROOT_DIR}/test/*.c # For tests
)

set(TobudOS_INC 
    ${TobudOS_KNL_DIR}/core/include
    ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/cortex-m0+/gcc
    ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/common/include
    ${TobudOS_KNL_DIR}/osal/cmsis_os
    ${TobudOS_ROOT_DIR}/test/include # For tests
)

add_executable(tobudos
    ${TobudOS_PORT_SRC}
    ${TobudOS_SYS_SRC}
    k_main.cpp
)

target_compile_options(tobudos PRIVATE
    -Wno-write-strings
)

target_link_options(tobudos PRIVATE
    -Wl,--defsym,isr_pendsv=PendSV_Handler
    -Wl,--defsym,isr_systick=SysTick_Handler
)


pico_set_program_name(tobudos "tobudos")
pico_set_program_version(tobudos "0.1")

# Modify the below lines to enable/disable output over UART/USB
pico_enable_stdio_uart(tobudos 1)
pico_enable_stdio_usb(tobudos 1)

# Add the standard library to the build
target_link_libraries(tobudos
        pico_stdlib)

# Add the standard include files to the build
target_include_directories(tobudos PRIVATE
  ${CMAKE_CURRENT_LIST_DIR}
  ${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required
  ${PICO_SDK_PATH}/src/rp2_common/cmsis/stub/CMSIS/Core/Include
  ${PICO_SDK_PATH}/src/rp2_common/cmsis/stub/CMSIS/DEVICE/${PICO_CMSIS_DEVICE}/Include
  ${TobudOS_INC}
)

# Add any user requested libraries
target_link_libraries(tobudos 
        hardware_timer
        hardware_watchdog
        hardware_clocks
        )

pico_add_extra_outputs(tobudos)

